/**
 * @Project TMHSCFX
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Apr 5, 2011
 * @Createtime 11:35:02 AM
 */

package com.tmh.study.dF4C.components
{
	import flash.display.Graphics;
	import flash.display.Sprite;

	import mx.core.UIComponent;

	public class MyFirstComponent extends UIComponent
	{
		private var _backGroundColor:uint = 0xcc0000;

		private var _ellipse:Sprite;

		private static const DEFAULT_HEIGHT:Number = 200;

		private static const DEFAULT_WIDTH:Number = 200;

		public function MyFirstComponent()
		{
			super();
		}

		override protected function createChildren():void
		{
			super.createChildren();

			_ellipse = new Sprite();
			addChild(_ellipse);
		}

		override protected function commitProperties():void
		{
			super.commitProperties();

			invalidateDisplayList();
		}

		override protected function measure():void
		{
			super.measure();

			measuredMinHeight = measuredHeight = DEFAULT_HEIGHT;
			measuredMinWidth = measuredWidth = DEFAULT_WIDTH;
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			height = unscaledHeight;
			width = unscaledWidth;

			var g:Graphics = this.graphics;

			g.clear();
			g.beginFill(_backGroundColor, 1);
			g.drawRect(0, 0, width, height);
			g.endFill();

			var eg:Graphics = _ellipse.graphics;
			eg.clear();
			eg.beginFill(0x0000ff, 1);
			eg.drawEllipse(0, 0, unscaledWidth, unscaledHeight);
			eg.endFill();
		}

		public function get backGroundColor():uint
		{
			return _backGroundColor;
		}

		public function set backGroundColor(value:uint):void
		{
			_backGroundColor = value;

			invalidateProperties(); // invoke commitProperties during next render phase to change component visual
		}

	}
}